package com.timemanage.dao

import androidx.room.Dao
import androidx.room.Insert
import androidx.room.Query
import androidx.room.Update
import com.timemanage.entity.User
import com.timemanage.entity.Work

@Dao
interface WorkDao {
    @Insert
    fun insertWork(work: Work?):Long

    @Update
    fun update(newUser: User)

    //获取用户所有任务
    @Query("select * from Work where userId=:userId")
    fun selectAll(userId:Int):List<Work>

    //获取第一层任务分类
    @Query("select * from Work where isParent=1 and parentId=-1")
    fun selectMainType():List<Work>

    //获取最后一层任务
    @Query("select * from Work where isParent=0")
    fun selectbaseType():List<Work>

    //查询某个任务
    @Query("select * from Work where workId=:id")
    fun selectOneWork(id:Int):Work

    //获取下一层任务分类
    @Query("select * from Work where parentId=:parentId")
    fun selectSon(parentId:Int):List<Work>

    //获取下一层任务分类
    @Query("select * from Work where parentId=:parentId and userId=:userId and startTime is null")
    fun selectSonType(userId: Int,parentId:Int):List<Work>

    //根据时间查询用户任务
    @Query("select * from Work where userId=:userId and isParent=0 and parentId =:parentId and startTime>=:startTime and endTime<=:endTime ")
    fun selectByTime(userId: Int,parentId: Int,startTime:Long,endTime:Long):List<Work>

    //删除用户的一个任务
    @Query("delete from Work where userId=:userId and workId=:workId")
    fun deleteUserWork(userId: Int,workId:Int):Int

    //删除一个分类
    @Query("delete from Work where workId=:workId")
    fun deleteWorkType(workId:Int):Int

    //查询用户每天花费时间
    @Query("select * from Work where userId=:userId and isParent=0 and startTime>=:startTime  ")
    fun selectDayTime(userId: Int,startTime:Long):List<Work>

    //更新结束时间
    @Query("update Work set endTime=:endTime where workId=:workId")
    fun updateEndTime(workId: Int,endTime: Long)

    //获取代办事件
    @Query("select * from Work where isParent=0 and startTime>:nowTime")
    fun selectTodo(nowTime: Long):List<Work>

}